{{ define "css"}}
<link href="https://cdn.bootcss.com/limonte-sweetalert2/7.33.1/sweetalert2.min.css" rel="stylesheet">
<style type="text/css">
    body {background: #e5e5e5;}
</style>
{{ end }}

{{define "content"}}

    <div class="container" style="padding: 2rem 0rem;">
        <div class="columns body-columns">
            <div class="column is-one-quarter">
                <div class="box">
                    {{include "user/sidebar"}}
                </div>
            </div>
            <div class="column">
                <div class="card">
                    <header class="card-header">
                        <p class="card-header-title">
                            基本信息
                        </p>
                    </header>
                    <div class="card-content">
                        <div class="content">
                            <form id="app" class="form-signin">
                                <div class="field">
                                    <div class="control">
                                        <label class="label">姓名</label>
                                        <input type="text" name="real_name" class="input" id="real_name" placeholder="姓名" v-model="real_name">
                                    </div>
                                </div>
                                <div class="field">
                                    <div class="control">
                                        <label class="label">Email</label>
                                        <input type="text" name="real_name" class="input" id="real_name" value="{{.user.Email}}" disabled="disabled">
                                    </div>
                                </div>
                                <div class="field">
                                    <div class="control">
                                        <label class="label">选择上传头像</label>
                                        <input type="file" name="avatar" class="input" id="avatar">
                                    </div>
                                </div>
                                <div class="field">
                                    <div class="control">
                                        <label class="label">简介</label>
                                        <textarea class="textarea" name="introduction" id="introduction" v-model="introduction"></textarea>
                                    </div>
                                </div>
                                <button class="button is-block is-info is-medium is-fullwidth" @click="doUpdate">更新信息</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{{end}}

{{ define "js"}}
<script src="https://cdn.bootcss.com/limonte-sweetalert2/7.33.1/sweetalert2.all.min.js"></script>
<script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data: {
            message: 'Hello Vue!',
            introduction: {{.user.Introduction}},
            real_name: {{.user.RealName}},
            avatar: {{.user.Avatar}},
        },
        delimiters:['${', '}'],
        methods: {
            doUpdate: function (e) {
                e.preventDefault();
                var bodyFormData = new FormData();
                bodyFormData.set('introduction', this.introduction);
                bodyFormData.set('real_name', this.real_name);
                bodyFormData.set('avatar', $('#avatar')[0].files[0]);

                let config = {
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                };
                this.$http.post("/settings/basic", bodyFormData, config).then((response) => {
                    console.log(response);
                    if (response.data.code === 0) {
                        // http://mishengqiang.com/sweetalert/
                        swal("更新成功！", "","success");
                        $('#imgAvatar').value(response.data.data.url);
                        return true;
                    } else {
                        swal("修改失败！", "","error");
                        return false;
                    }
                }).catch( error => {
                    console.log(error);
                });
            }
        }
    })
</script>
{{ end }}